package pl.edu.agh.ki.ewywiadowka.client.windows;

import java.util.LinkedList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gen2.table.client.FixedWidthFlexTable;
import com.google.gwt.gen2.table.client.FixedWidthGrid;
import com.google.gwt.gen2.table.client.ScrollTable;
import com.google.gwt.gen2.table.client.AbstractScrollTable.SortPolicy;
import com.google.gwt.gen2.table.override.client.HTMLTable.RowFormatter;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.TextBox;

import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.Const.FormularMessages;
import pl.edu.agh.ki.ewywiadowka.client.dto.AttendantDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.ClassDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.StudentDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.secretary.ModifyAttendantWindow;
import pl.edu.agh.ki.ewywiadowka.client.windows.secretary.ModifyStudentWindow;

/**
 * w zaleznosci od mode ta klasa pozwala na: 1 - modyfikacja opiekuna 2 -
 * usuniecie opiekuna
 * 
 * @author pimol
 * 
 */
public class SearchAttendantWindow extends GenericWindow implements
		ClickHandler, TableListener {

	private ScrollPanel _mainPanel = new ScrollPanel();
	private DockPanel _dockPanel = new DockPanel();
	private HorizontalPanel _filterHorizontalPanel = new HorizontalPanel();

	private Grid _formular;
	private ScrollTable _scrollTable = null;
	private Button _okButton = new Button("Wybierz opiekuna");
	private Button _filterButton = new Button("Filtruj");

	private TextBox _nameTextBox = new TextBox();
	private TextBox _surnameTextBox = new TextBox();

	private FixedWidthGrid _dataTable;
	private FixedWidthFlexTable _headerTable;

	private int _selectedRow = -1;

	private SearchAttendantWindow _me;

	private LinkedList<AttendantDTO> _attendants = null; // bo na poczatku nie
	// ma

	private int _mode = -1;

	public SearchAttendantWindow(EWywiadowka wywiadowka, int mode) {
		super(wywiadowka);

		_me = this;

		_mode = mode;

		_formular = new Grid(2, 2);
		_formular.setWidget(0, 0, new Label("Imię"));
		_formular.setWidget(1, 0, new Label("Nazwisko"));

		_formular.setWidget(0, 1, _surnameTextBox);
		_formular.setWidget(1, 1, _nameTextBox);

		_filterButton.addClickHandler(this);
		_okButton.addClickHandler(this);
		
		_filterHorizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		_filterHorizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		_filterHorizontalPanel.add(_formular);
		_filterHorizontalPanel.add(_filterButton);

		_dataTable = new FixedWidthGrid(0, 4);
		_dataTable.setBorderWidth(1);

		_headerTable = new FixedWidthFlexTable();
		_headerTable.setBorderWidth(1);
		fillHeader();

		_scrollTable = new ScrollTable(_dataTable, _headerTable);
		_scrollTable.fillWidth();
		_scrollTable.setHeight("400px");

		_dockPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		_dockPanel.add(_filterHorizontalPanel, DockPanel.NORTH);
		_dockPanel.add(_scrollTable, DockPanel.CENTER);
		_dockPanel.add(_okButton, DockPanel.SOUTH);

		_mainPanel.add(_dockPanel);
		
		this.initWidget(_mainPanel);
	}

	private void fillRow(int row, AttendantDTO attendant) {

		RowFormatter rowFormatter = _dataTable.getRowFormatter();

		if (row % 2 == 0)
			rowFormatter.addStyleName(row, "tableRowStyle1");
		else
			rowFormatter.addStyleName(row, "tableRowStyle2");

		_dataTable.setHTML(row, 0, attendant.name.toString());
		_dataTable.setHTML(row, 1, attendant.surname.toString());
		_dataTable.setHTML(row, 2, attendant.address.toString());
		_dataTable.setHTML(row, 3, attendant.telephone.toString());
	}

	private void fillHeader() {
		_headerTable.setHTML(0, 0, "Imię");
		_headerTable.setHTML(0, 1, "Nazwisko");
		_headerTable.setHTML(0, 2, "Adres");
		_headerTable.setHTML(0, 3, "Nr telefonu");
	}

	private void repaintTable() {
		int i = 0;
		for (AttendantDTO attendant : _attendants)
			fillRow(i++, attendant);
	}

	private FormularMessages verifyFormular() {
		return FormularMessages.OK;
	}

	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource() == _filterButton) {
			Const.FormularMessages msg;
			if ((msg = verifyFormular()) == Const.FormularMessages.OK)
				getAttendants();
			else
				Window.alert(msg.toString());
			
		} else if (event.getSource() == _okButton) {
			
			if (_selectedRow == -1) {
				Window.alert("Prosze wybrac opiekuna");
			} else {
				final AttendantDTO attendant = _attendants.get(_selectedRow);
				Window.alert("Wybrano opiekuna: \n" + attendant.toString());

				_mainPanel.remove(_dockPanel);

				if (_mode == 1) { // modyfikacja
					_mainPanel.add(new ModifyAttendantWindow(getWywiadowka(),
							attendant));
				} else if (_mode == 2) { // usuniecie
					Const.eWywiadowkaService.removeAttendant(attendant,
							new AsyncCallback<Const.Error.AttendantRemoveMsg>() {

								@Override
								public void onFailure(Throwable caught) {
									Window.alert(caught.getMessage());
								}

								@Override
								public void onSuccess(Const.Error.AttendantRemoveMsg result) {
									Window.alert(result.toString());
									_mainPanel.add(new SearchAttendantWindow(
											getWywiadowka(), 2));
								}

							});
				}
			}
		}

	}

	private void getAttendants() {
		Const.eWywiadowkaService
				.getAttendants(new AsyncCallback<LinkedList<AttendantDTO>>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}

					@Override
					public void onSuccess(LinkedList<AttendantDTO> result) {
						_attendants = result;

						_headerTable = new FixedWidthFlexTable();
						_headerTable.setBorderWidth(1);
						fillHeader();

						_dataTable = new FixedWidthGrid(_attendants.size(), 4);
						_dataTable.setBorderWidth(1);
						repaintTable();

						_dockPanel.remove(_scrollTable);

						_scrollTable = new ScrollTable(_dataTable, _headerTable);
						_scrollTable.setHeight("400px");
						_scrollTable.setVisible(true);

						_dataTable.addTableListener(_me);
						_scrollTable.setSortPolicy(SortPolicy.DISABLED);

						_dockPanel.add(_scrollTable, DockPanel.CENTER);
						_scrollTable.fillWidth();
					}

				});
	}

	@Override
	public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
		RowFormatter rowFormatter = _dataTable.getRowFormatter();
		if (_selectedRow != -1) {
			if (_selectedRow % 2 == 0)
				rowFormatter.setStyleName(_selectedRow, "tableRowStyle1");
			else
				rowFormatter.setStyleName(_selectedRow, "tableRowStyle2");
		}
		_selectedRow = row;
		rowFormatter.setStyleName(row, "clickedTableRowStyle");
	}

}
